import java.awt.*;
import java.awt.event.*;

import javax.swing.*;

import java.io.*;
import java.awt.image.*; 
import javax.imageio.*; 
import javax.swing.border.*;


public class LayeredGameBoardPanel extends JPanel
{
	enum Move {left, up, right, down}
	private Timer timer1, timer2, timer3, timer4;
	private int x,y, x1, y1, x2, y2, x3, y3, x4, y4;


	private final int DELAY = 0, IMAGE_SIZE = 25;
	private final int WIDTH = 695;
	private final int HEIGHT = 695;
	
	private int w1, h1, w2, h2, w3, h3, w4, h4;


	private PawnPanel ppDog, ppBoat, ppWBarrow, ppHat;
	private GameBoardPanel gbp;
	
	public LayeredGameBoardPanel()
	{
		setBackground(Color.black);
		
		gbp = new GameBoardPanel();
        gbp.setOpaque(false);
        gbp.setBounds(0, 0, 695, 695);
				
	    JLayeredPane lp = new JLayeredPane();	    
	    lp.setPreferredSize(new Dimension(WIDTH, HEIGHT));
	    lp.setOpaque(false);
	    
		timer1 = new Timer(DELAY, new PawnListener());
		timer2 = new Timer(DELAY, new PawnListener());
		timer3 = new Timer(DELAY, new PawnListener());
		timer4 = new Timer(DELAY, new PawnListener());

		x1 = 630; // in Go
		y1 = 630; // in Go
		x2 = 658;
		y2 = 630;
		x3 = 630;
		y3 = 658;
		x4 = 658;
		y4 = 658;
	

		
        ppBoat = new PawnPanel("boat");
        ppBoat.setPlayerNum(1);
        ppBoat.setOpaque(false);
        ppBoat.setBounds(x1, y1, WIDTH, HEIGHT);
        ppBoat.initializePlacementArray();
        ppBoat.setNewBoardLocation(0);         //Change this to test the path and location finding for the boat icon
        
        ppWBarrow = new PawnPanel("wheelbarrow");
        ppWBarrow.setPlayerNum(2);
        ppWBarrow.setOpaque(false);
        ppWBarrow.setBounds(x2, y2, WIDTH, HEIGHT);
        ppWBarrow.initializePlacementArray();
        ppWBarrow.setNewBoardLocation(0);
        
        ppDog = new PawnPanel("dog");
        ppDog.setPlayerNum(3);
        ppDog.setOpaque(false);
        ppDog.setBounds(x3, y3, WIDTH, HEIGHT);
        ppDog.initializePlacementArray();
        ppDog.setNewBoardLocation(0);
        
        ppHat = new PawnPanel("hat");
        ppHat.setPlayerNum(4);
        ppHat.setOpaque(false);
        ppHat.setBounds(x4, y4, WIDTH, HEIGHT);
        ppHat.initializePlacementArray();
        ppHat.setNewBoardLocation(0);
        
	timer1.start();
	timer2.start();
	timer3.start();
	timer4.start();
		
	lp.add(gbp, new Integer(0));
	lp.add(ppDog, new Integer(1));
	lp.add(ppWBarrow, new Integer(1));
	lp.add(ppBoat, new Integer(1));
	lp.add(ppHat, new Integer(1));
		
	add(lp);
	} // end constructor
	
	public void movePawn(int loc, int playerNum)
	{
/*		if(loc < 40)
		{
			if(playerNum == 1) ppBoat.setNewBoardLocation(loc);
			else if (playerNum == 2) ppWBarrow.setNewBoardLocation(loc);
			else if (playerNum == 3) ppDog.setNewBoardLocation(loc);
			else if (playerNum == 4) ppHat.setNewBoardLocation(loc);
		}
*/
		if(loc < 40)
		{
			if(playerNum == 1)
			{
				timer1.stop();
				ppBoat.setVisible(false);
				ppBoat.setNewBoardLocation(loc);
				x1 = ppBoat.getNewLocationX();
				y1 = ppBoat.getNewLocationY();
				ppBoat.setVisible(true);
				timer1.start();
				
			}
			else if(playerNum == 2)
			{
				timer2.stop();
				ppWBarrow.setVisible(false);
				ppWBarrow.setNewBoardLocation(loc);
				x2 = ppWBarrow.getNewLocationX();
				y2 = ppWBarrow.getNewLocationY();
				ppWBarrow.setVisible(true);
				timer2.start();
			}
			else if(playerNum == 3)
			{
				timer3.stop();
				ppDog.setVisible(false);
				ppDog.setNewBoardLocation(loc);
				x3 = ppDog.getNewLocationX();
				y3 = ppDog.getNewLocationY();
				ppDog.setVisible(true);
				timer3.start();
			}
			else if(playerNum == 4)
			{
				timer4.stop();
				ppHat.setVisible(false);
				ppHat.setNewBoardLocation(loc);
				x4 = ppHat.getNewLocationX();
				y4 = ppHat.getNewLocationY();
				ppHat.setVisible(true);
				timer4.start();
			}
		}
		
		
		else if(loc == 40)
		{
			if(playerNum == 1)
			{
				timer1.stop();
				ppBoat.setVisible(false);
				ppBoat.setNewBoardLocation(10);
				x1 = ppBoat.getNewLocationX();
				y1 = ppBoat.getNewLocationY();
				ppBoat.setVisible(true);
				timer1.start();
				
			}
			else if(playerNum == 2)
			{
				timer2.stop();
				ppWBarrow.setVisible(false);
				ppWBarrow.setNewBoardLocation(10);
				x2 = ppWBarrow.getNewLocationX();
				y2 = ppWBarrow.getNewLocationY();
				ppWBarrow.setVisible(true);
				timer2.start();
			}
			else if(playerNum == 3)
			{
				timer3.stop();
				ppDog.setVisible(false);
				ppDog.setNewBoardLocation(10);
				x3 = ppDog.getNewLocationX();
				y3 = ppDog.getNewLocationY();
				ppDog.setVisible(true);
				timer3.start();
			}
			else if(playerNum == 4)
			{
				timer4.stop();
				ppHat.setVisible(false);
				ppHat.setNewBoardLocation(10);
				x4 = ppHat.getNewLocationX();
				y4 = ppHat.getNewLocationY();
				ppHat.setVisible(true);
				timer4.start();
			}
		}
		
	}
	
	public boolean pawnsDone() //Used to paused gameplay until this is true
	
	{
		boolean done = false;
		if((ppBoat.getNewLocationX() == x1 && ppBoat.getNewLocationY() == y1) &&
		(ppWBarrow.getNewLocationX() == x2 && ppWBarrow.getNewLocationY() == y2) &&
		(ppDog.getNewLocationX() == x3 && ppDog.getNewLocationY() == y3) &&
		(ppHat.getNewLocationX() == x4 && ppHat.getNewLocationY() == y4))
		{
			done = true;
		}
		return done;
	}
	
	public void paintComponent (Graphics g)
	{
		//g.drawImage(gameboard.getImage(),0,0,null);
		super.paintComponent (g);
		ppBoat.setBounds(x1,y1,695,695);
		ppWBarrow.setBounds(x2,y2,695,695);
		ppDog.setBounds(x3,y3,695,695);
		ppHat.setBounds(x4,y4,695,695);
		
		//pawn_dog.paintIcon(this, g, x, y);
		//pawn_hat.paintIcon(this, g, 10, 10);
		//pawn_boat.paintIcon(this, g, 10, 640);
		//pawn_wbarrow.paintIcon(this, g, 640, 10);
   	}
	private class PawnListener implements ActionListener
	{
		//--------------------------------------------------------------
		//  Updates the position of the image and possibly the direction
		//  of movement whenever the timer fires an action event.
		//--------------------------------------------------------------
		public void actionPerformed (ActionEvent event)
		{
			Object source = event.getSource();
			if(source == timer1)
			{
				if(ppBoat.getNewLocationX() != x1 || ppBoat.getNewLocationY() != y1)
				{
					if(ppBoat.getDirection() == "left")
					{			
						x1 += -1;
						y1 += 0;
					}
					else if(ppBoat.getDirection() == "up")
					{			
						x1 += 0;
						y1 += -1;
					}
					else if(ppBoat.getDirection() == "right")
					{			
						x1 += 1;
						y1 += 0;
					}
					else if(ppBoat.getDirection() == "down")
					{			
						x1 += 0;
						y1 += 1;
					}
	
				//if(x1 <= 545)
					//timer1.stop();
				
				//if (x1 <= 15 && ppBoat.getDirection() == "left")
					if (x1 <= 12 && ppBoat.getDirection() == "left")
					{
						//System.out.println("x= " + x);
						//System.out.println("dir= " + ppBoat.getDirection());
						ppBoat.setDirection("up");
					}	
					if (y1 <= 12 && ppBoat.getDirection() == "up")
					{	
						//System.out.println("'y= " + y);
						//System.out.println("dir= " + ppBoat.getDirection());
						ppBoat.setDirection("right");
					}				
					if(x1 >= 655-IMAGE_SIZE && ppBoat.getDirection() == "right")
					{	
						//System.out.println("x= " + x);
						//System.out.println("dir= " + ppBoat.getDirection());
						ppBoat.setDirection("down");
					}
					if(y1 >= 655-IMAGE_SIZE && ppBoat.getDirection() == "down")
					{
						//System.out.println("'y= " + y);
						//System.out.println("dir= " + ppBoat.getDirection());
						ppBoat.setDirection("left");
						//timer1.stop();
					}
				} // end if boat currentlocation != newlocation
			} // end if source == timer1
			else if(source == timer2)
			{
				if(ppWBarrow.getNewLocationX() != x2 || ppWBarrow.getNewLocationY() != y2)
				{
					if(ppWBarrow.getDirection() == "left")
					{			
						x2 += -1;
						y2 += 0;
					}
					else if(ppWBarrow.getDirection() == "up")
					{			
						x2 += 0;
						y2 += -1;
					}
					else if(ppWBarrow.getDirection() == "right")
					{			
						x2 += 1;
						y2 += 0;
					}
					else if(ppWBarrow.getDirection() == "down")
					{			
						x2 += 0;
						y2 += 1;
					}
				}
	
				if (x2 <= 40 && ppWBarrow.getDirection() == "left")
				{
					ppWBarrow.setDirection("up");
				}	
				if (y2 <= 12 && ppWBarrow.getDirection() == "up")
				{	
					ppWBarrow.setDirection("right");
				}
				if(x2 >= 658 && ppWBarrow.getDirection() == "right")
				{	
					ppWBarrow.setDirection("down");
				}
				if(y2 >= 630 && ppWBarrow.getDirection() == "down")
				{
					ppWBarrow.setDirection("left");
					//timer2.stop();
				}
			} // end if source == timer2
			else if(source == timer3)
			{
				if(ppDog.getNewLocationX() != x3 || ppDog.getNewLocationY() != y3)
				{
					if(ppDog.getDirection() == "left")
					{			
						x3 += -1;
						y3 += 0;
					}
					else if(ppDog.getDirection() == "up")
					{			
						x3 += 0;
						y3 += -1;
					}
					else if(ppDog.getDirection() == "right")
					{			
						x3 += 1;
						y3 += 0;
					}
					else if(ppDog.getDirection() == "down")
					{			
						x3 += 0;
						y3 += 1;
					}
				}
				
				if (x3 <= 12 && ppDog.getDirection() == "left")
				{
					ppDog.setDirection("up");
				}	
				if (y3 <= 40 && ppDog.getDirection() == "up")
				{	
					ppDog.setDirection("right");
				}
				//if(x3 >= 655-IMAGE_SIZE && ppDog.getDirection() == "right")
				if(x3 == 630 && ppDog.getDirection() == "right")
				{	
					ppDog.setDirection("down");
				}
				//if(y3 >= 685-IMAGE_SIZE && ppDog.getDirection() == "down")
				if(y3 >= 658 && ppDog.getDirection() == "down")
				{
					ppDog.setDirection("left");
					//timer3.stop();
				}
			} // end if source == timer3
			else if(source == timer4)
			{
				if(ppHat.getNewLocationX() != x4 || ppHat.getNewLocationY() != y4)
                                {
					if(ppHat.getDirection() == "left")
					{			
						x4 += -1;
						y4 += 0;
					}
					else if(ppHat.getDirection() == "up")
					{			
						x4 += 0;
						y4 += -1;
					}
					else if(ppHat.getDirection() == "right")
					{			
						x4 += 1;
						y4 += 0;
					}
					else if(ppHat.getDirection() == "down")
					{			
						x4 += 0;
						y4 += 1;
					}
				}
	
				if (x4 <= 40 && ppHat.getDirection() == "left")
				{
					ppHat.setDirection("up");
				}	
				if (y4 <= 40 && ppHat.getDirection() == "up")
				{	
					ppHat.setDirection("right");
				}
				if(x4 >= 658 && ppHat.getDirection() == "right")
				{	
					ppHat.setDirection("down");
				}
				if(y4 >= 658 && ppHat.getDirection() == "down")
				{
					ppHat.setDirection("left");
					//timer4.stop();
				}
			} // end if source == timer4
			repaint();
		} // end function actionPerformed
	} // end class ReboundListener

/*	
	public int calcDistance(int oldLoc, int newLoc)
	{
		int retDist = 0;
		if(oldLoc <= 10)
		{
//			retDist = newLoc
		}
	}
	*/

	
	public void buildHouses(int num, String dir, String name)
	{
		gbp.buildHouses(num, dir, name);
	}
	
	public void removeAllHouses(String dir, String name)
	{
		gbp.removeAllHouses(dir, name);
	}
	
	public void buildHotel(String dir, String name)
	{
		gbp.buildHotel(dir, name);
	}
	
	
} // end class LayeredGameBoardPanel
